//
//  AddMPDetailTableViewController.m
//  TimeManager
//
//  Created by 刘凯敏 on 20/12/2016.
//  Copyright © 2016 刘凯敏. All rights reserved.
//

#import "AddMPDetailTableViewController.h"

@implementation AddMPDetailViewController
@synthesize textLabel;
@synthesize startDateField;
@synthesize endDateField;
@synthesize startDatePicker;
@synthesize endDatePicker;

- (void)viewDidLoad {
    [super viewDidLoad];
    //为tableView控件设置datasource和delegate
    self.addTableView.dataSource = self;
    self.addTableView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource
//该方法的返回值决定各表格行的控件
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //为表格行定义一个静态字符串作为标识符
    static NSString *cellId = @"cellId";
    //从可重用表格行队列中取出一个表格行
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    //创建cell对象
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    //获取当前时间
    NSDate *localDate = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    //设置自定义的格式模板
    [df setDateFormat:@"yyyy年MM月dd日"];
    NSLocale *dateLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_zn"];
    
    if(indexPath.row == 0){
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, 290, 30)];
        textField.font = [UIFont systemFontOfSize:16];
        textField.placeholder = @"标题";
        //边框样式
        textField.layer.borderColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00].CGColor;
        textField.layer.borderWidth = 0.5;
        textField.layer.cornerRadius = 6;
        textField.layer.masksToBounds = YES;
        
        [cell.contentView addSubview:textField];
        return cell;
    }
    else if (indexPath.row == 1){
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, 290, 30)];
        textField.font = [UIFont systemFontOfSize:16];
        textField.placeholder = @"地点";
        //边框样式
        textField.layer.borderColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00].CGColor;
        textField.layer.borderWidth = 0.5;
        textField.layer.cornerRadius = 6;
        textField.layer.masksToBounds = YES;
        
        [cell.contentView addSubview:textField];
        return cell;
    }
    else if (indexPath.row == 2){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 70, 30)];
        label.text = @"开始时间";
        label.font = [UIFont systemFontOfSize:16];
        [cell.contentView addSubview:label];
        
        startDateField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        startDateField.text = [df stringFromDate:localDate];
        startDateField.font = [UIFont systemFontOfSize:16];
        
        startDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 10, 0, 0)];
        startDatePicker.locale = dateLocale;
        //设置UIDatePicker显示模式
        startDatePicker.datePickerMode = UIDatePickerModeDate;
        startDateField.inputView = startDatePicker;
        
        //设置最小时间
        NSDate* minDate = [[NSDate alloc] initWithTimeIntervalSince1970:0];
        //设置最大时间
        NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSince1970:3600*24*366*100];
        //设置属性
        startDatePicker.minimumDate = minDate;
        startDatePicker.maximumDate = maxDate;
        
        [startDatePicker addTarget:self action:@selector(startDateChanged:) forControlEvents:UIControlEventValueChanged ];
        [cell.contentView addSubview:startDateField];
        return cell;
    }
    else if (indexPath.row == 3){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 70, 30)];
        label.text = @"结束时间";
        label.font = [UIFont systemFontOfSize:16];
        [cell.contentView addSubview:label];
        
        endDateField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 185, 30)];
        endDateField.text = [df stringFromDate:localDate];
        endDateField.font = [UIFont systemFontOfSize:16];
        
        endDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 10, 0, 0)];
        endDatePicker.locale = dateLocale;
        //设置UIDatePicker显示模式
        endDatePicker.datePickerMode = UIDatePickerModeDate;
        endDateField.inputView = endDatePicker;
        
        //设置最小时间
        NSDate* minDate = [[NSDate alloc] initWithTimeIntervalSince1970:0];
        //设置最大时间
        NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSince1970:3600*24*366*100];
        //设置属性
        endDatePicker.minimumDate = minDate;
        endDatePicker.maximumDate = maxDate;
        
        [endDatePicker addTarget:self action:@selector(endDateChanged:) forControlEvents:UIControlEventValueChanged ];
        [cell.contentView addSubview:endDateField];
        return cell;
    }
    else if (indexPath.row == 4){
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 30)];
        textLabel.enabled = NO;
        textLabel.text = @"描述";
        textLabel.font = [UIFont systemFontOfSize:15];
        textLabel.textColor = [UIColor lightGrayColor];
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 150)];
        textView.font = [UIFont systemFontOfSize:15];
        [textView addSubview:textLabel];
        textView.delegate = self;
        [cell.contentView addSubview:textView];
        return cell;
    }
    else return nil;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

//该方法的返回值决定指定分区内包含多少个表格行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //由于只有一个分区，直接返回表格的行数
    return 5;
}

//该方法的返回值将作为表格行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row != 4){
        return 50;
    }
    else return 150;
}

#pragma mark - UITextViewDelegate
- (void) textViewDidChange:(UITextView *)textView{
    if(textView.text.length == 0){
        [textLabel setHidden:NO];
    }
    else [textLabel setHidden:YES];
}

-(void)startDateChanged:(id)sender{
    //NSDate格式转换为NSString格式
    NSDate *pickerDate = [self.startDatePicker date];// 获取用户通过UIDatePicker设置的日期和时间
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateString = [pickerFormatter stringFromDate:pickerDate];
    self.startDateField.text = dateString;
}

-(void)endDateChanged:(id)sender{
    //NSDate格式转换为NSString格式
    NSDate *pickerDate = [self.endDatePicker date];// 获取用户通过UIDatePicker设置的日期和时间
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateString = [pickerFormatter stringFromDate:pickerDate];
    self.endDateField.text = dateString;
}


@end
